package leetcode.primary;

import java.util.ArrayList;
import java.util.List;

public class T77 {
    public List<List<Integer>> ans;
    public List<List<Integer>> combine(int n, int k) {
        ans = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(1,n,k,list);
        return ans;
    }

    public void dfs(int temp,int n,int k,List<Integer> list){
        if (list.size()==k){
            ans.add(new ArrayList<>(list));
            return;
        }
        for (int i = temp; i <= n; i++) {
            if (list.size()+n-i+1<k){
                break;
            }
            list.add(i);
            dfs(i+1,n,k,list);
            list.remove(list.size()-1);
        }
    }

    public static void main(String[] args) {
        T77 test = new T77();
        List<List<Integer>> combine = test.combine(4, 2);
        for (List<Integer> list : combine) {
            for (Integer integer : list) {
                System.out.print(integer+" ");
            }
            System.out.println();
        }
    }
}
